/* (c)2010-2011 Andrea Caltroni (andrea.caltroni@pd.infn.it), INFN (www.infn.it)
 *
 * This file is part of the IGI Survey Portlet (from here on shortened to IGISP).
 * IGISP is free software: you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) any 
 * later version.
 *
 * IGISP is distributed in the hope that it will be useful, but WITHOUT ANY 
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with IGISP. If not, see <http://www.gnu.org/licenses/>.
 */
package it.infn.grid.liferay.survey.modules.admin.web;

import java.util.SortedSet;
import java.util.TreeSet;

import javax.persistence.CascadeType;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;

import org.ajm.spring.core.model.AbstractPersistentEntity;
import org.ajm.spring.survey.modules.admin.model.Question;
import org.ajm.spring.survey.modules.admin.model.QuestionPositionComparator;
import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.hibernate.annotations.Sort;
import org.hibernate.annotations.SortType;

/**
 * The command object used to bind the values in the form to change the order of
 * questions.
 * 
 * @author Andrea Caltroni, andrea.caltroni@pd.infn.it, INFN
 */
public class ChangeQuestionOrderCommand extends AbstractPersistentEntity implements Comparable<ChangeQuestionOrderCommand> {
    
    // ------------------------------------------------------------------------|
    // --- CONSTANTS ----------------------------------------------------------|
    // ------------------------------------------------------------------------|

    private static final long serialVersionUID = -5024439285515085937L;

    // ------------------------------------------------------------------------|
    // --- CLASS VARIABLES ----------------------------------------------------|
    // ------------------------------------------------------------------------|

    // ------------------------------------------------------------------------|
    // --- INSTANCE VARIABLES -------------------------------------------------|
    // ------------------------------------------------------------------------|

    private SortedSet<Question> questions = new TreeSet<Question>(new QuestionPositionComparator());

    // ------------------------------------------------------------------------|
    // --- CONSTRUCTORS -------------------------------------------------------|
    // ------------------------------------------------------------------------|

    /**
     * Default empty constructor.
     */
    public ChangeQuestionOrderCommand() {
        
        super();
    }

    // ------------------------------------------------------------------------|

    /**
     * @param questions
     */
    public ChangeQuestionOrderCommand(SortedSet<Question> questions) {
        
        super();
        if (questions != null) this.questions = questions;
    }

    // ------------------------------------------------------------------------|
    // --- CLASS METHODS ------------------------------------------------------|
    // ------------------------------------------------------------------------|

    // ------------------------------------------------------------------------|
    // --- INSTANCE METHODS ---------------------------------------------------|
    // ------------------------------------------------------------------------|
    
    /**
     * Adds the ability to perform order comparisons.
     * 
     * @see java.lang.Comparable#compareTo(java.lang.Object)
     */
    public int compareTo(ChangeQuestionOrderCommand that) {
        
        return new CompareToBuilder()

        .append(this.getDateCreated(), that.getDateCreated())
        
        .append(this.getQuestions(), that.getQuestions())
        
        .toComparison();

    }
    
    // ------------------------------------------------------------------------|
    
    /**
     * Implements the concept of logical equality for this object.
     *
     * @see java.lang.Object#hashCode()
     * @see java.lang.Object#equals(Object)
     */
    @Override
    public boolean equals(Object object) {

        if (this == object)
            return true;

        if (!(object instanceof ChangeQuestionOrderCommand)) {
            return false;
        }

        final ChangeQuestionOrderCommand that = (ChangeQuestionOrderCommand) object;

        return new EqualsBuilder()
        
        .append(this.getQuestions(), that.getQuestions())

        .isEquals();
    }

    // ------------------------------------------------------------------------|

    /**
     * Compute the hash code for this object. This is necessary when overriding
     * equals since equal objects must have equal hash codes.
     * 
     * @see java.lang.Object#hashCode()
     * @see java.lang.Object#equals(Object)
     */
    @Override
    public int hashCode() {

        return new HashCodeBuilder(-83574467, 150340475)

        .append(this.getQuestions())

        .toHashCode();
    }

    // ------------------------------------------------------------------------|

    /**
     * Obtain a string representation of this object.
     *
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {

        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)

        .append("questions ", this.getQuestions())

        .toString();
    }

    // ------------------------------------------------------------------------|
    
    /**
     * @return the questions
     */
    @OneToMany(mappedBy = "survey", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval=true)
    @Sort(type = SortType.COMPARATOR, comparator = QuestionPositionComparator.class)
    public SortedSet<Question> getQuestions() {
        return this.questions;
    }

    // ------------------------------------------------------------------------|
    
    /**
     * @param questions the questions to set
     */
    public void setQuestions(SortedSet<Question> questions) {
        this.questions = questions;
    }

    // ------------------------------------------------------------------------|
    
    /*
     * @param question the question to set
     */
//    public void setQuestion(Question question) {
//        getQuestions().add(question);
//    }

    // ------------------------------------------------------------------------|
    
    /*
     * Checks if this survey has questions.
     * 
     * @return TRUE if this survey has questions. FALSE otherwise. 
     */
//    public boolean hasQuestions() {
//        
//        if (getQuestions().size() > 0) return true;
//        else return false;
//    }

    // ------------------------------------------------------------------------|
    
}
